package com.sh.fmbackend.once;

import com.sh.fmbackend.mapper.UserMapper;
import com.sh.fmbackend.model.domain.User;
import com.sh.fmbackend.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.util.StopWatch;
import sun.java2d.pipe.SpanClipRenderer;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import static com.sun.javafx.font.FontResource.SALT;

/**
 * @Auther: sh
 * @Date: 2024/12/22 18:10
 * @Description: 添加用户
 */
@Component
public class InsertUsers {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;

    private ExecutorService executorService=new ThreadPoolExecutor(20, 100, 10000, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1000));

    //    @Scheduled(fixedDelay=5000)
//    @EventListener
    public void doInsertUser(ContextRefreshedEvent event) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<User> list = new ArrayList<>();
        final int INSERT_COUNT = 10000000;
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUserName("sh" + i);
            user.setAvatarUrl("https://sh-hengyang.oss-cn-wuhan-lr.aliyuncs.com/user-center/1.GIF");
            user.setPlanetCode("1000");
            user.setPhone("13593828837");
            user.setTags("[\"男\",\"java\"]");
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + "12345678").getBytes());
            user.setUserPassword(encryptPassword);
            user.setIsDelete(0);
            user.setUserRole(0);
            user.setUserStatus(0);
            list.add(user);
        }
        userService.saveBatch(list, 1000);
        stopWatch.stop();
        double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
        System.out.println("总共耗时：" + totalTimeSeconds);
    }


    /**
     * 并发插入用户
     *
     * @param event
     */
//    @EventListener
    public void doConcurrencyInsertUser(ContextRefreshedEvent event) {

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        final int INSERT_COUNT = 10000;
        List<CompletableFuture<Void>> arrayList = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            List<User> list = new ArrayList<>();
            int j = 0;
            while (true) {
                j++;
                User user = new User();
                user.setUserName("sh" + j);
                user.setAvatarUrl("https://sh-hengyang.oss-cn-wuhan-lr.aliyuncs.com/user-center/1.GIF");
                user.setPlanetCode("1000");
                user.setPhone("13593828837");
                user.setTags("[\"男\",\"java\"]");
                String encryptPassword = DigestUtils.md5DigestAsHex((SALT + "12345678").getBytes());
                user.setUserPassword(encryptPassword);
                user.setIsDelete(0);
                user.setUserRole(0);
                user.setUserStatus(0);
                list.add(user);
                if (j % 400 == 0) {
                    break;
                }
            }
            // 一个任务
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
                System.out.println("当前线程：" + Thread.currentThread().getName());
                userService.saveBatch(list, 400);
            }, executorService);
            arrayList.add(voidCompletableFuture);
        }
        CompletableFuture.allOf(arrayList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();
        double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
        System.out.println("总共耗时：" + totalTimeSeconds);
    }


}

